package com.xcity.game.player.robot;

import java.util.ArrayList;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.buildv2.BuildManagerV2;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.buildv2.template.BuildTemplateV2;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.employee.EmployeeManager;
import com.xcity.game.player.PlayerUtils;
import com.xcity.game.player.robot.RobotTemplate.BuildingConfig;
import com.xcity.game.skill.SkillUtils;
import com.xcity.game.skill.v2.SkillManagerV2;

import naga.x.App;
import naga.x.db.type.PropertyPool;
import naga.x.game.template.TemplateService;

public class Robot extends Player {
	
	static PropertyPool defaultPool = new PropertyPool();
	protected RobotTemplate template;
	
	public Robot(RobotTemplate template) {
		this.id = template.getId();
		this.name = template.getName();
		this.icon = template.getIcon() != null ? template.getIcon() : (template.getSex() == Player.MALE ? Player.DEFAULT_ICON : Player.DEFAULT_FEMALE_ICON);
		this.sex = template.getSex();
//		this.exp = template.getExp();
		this.gold = template.getGold();
		this.stone = template.getGold();
		this.setBuildings2(new BuildManagerV2(this, new ArrayList<BuildV2>(2), 0, null));
		this.initBuildings(template.getWrappedBuildings());
		this.pool = defaultPool;
		this.setEmployeeManager(new EmployeeManager(this, null));
		this.level = template.getLevel();//template.getLevel() > 0 ? template.getLevel() : PlayerUtils.exp2Lvl(this.exp);
		this.exp = 0;
		this.fixedAssets = PlayerUtils.calcFixedAssets(this);
		SkillManagerV2 skillManager = new SkillManagerV2(this, SkillUtils.createActiveSkill(this), SkillUtils.createPassiveSkills(this));
		this.setSkillManager(skillManager);
		this.combatPower = CombatUtils.calcCombatPower(this);
		this.template = template;
	}
	
	private void initBuildings(List<BuildingConfig> buildingSettings) {
		if (buildingSettings == null) {
			return;
		}
		getBuildings2().getBuildings().clear();
		for (int i = 0; i < buildingSettings.size(); i++) {
			BuildingConfig setting = buildingSettings.get(i);
			int id = setting.id;
			int[] partsLvl = setting.partsLvl;
			List<BuildV2> buildings = getBuildings2().getBuildings();
			BuildTemplateV2 template = App.getApp().getServiceManager().get(TemplateService.class).get(BuildTemplateV2.class, id);
			BuildV2 building = new RobotBuild(getBuildings2(), template, setting);
			for (int j = 0; j < partsLvl.length; j++) {
				building.getPart(j).setLevel(partsLvl[j]);
				Robot.this.exp += template.getPartExp() * partsLvl[j];
			}
			if (building.isBuildDone()) {
				building.setState(com.xcity.game.buildv2.BuildV2.State.BUILT);
			} else {
				building.setState(com.xcity.game.buildv2.BuildV2.State.BUILDING);
			}
			buildings.add(building);
		}
	}

	@Override
	public boolean addGold(long gold, String cause) {
		return true;
	}

	@Override
	public boolean addStone(int stone, String cause) {
		return true;
	}

	@Override
	public boolean costGold(long gold, String cause) {
		return true;
	}

	@Override
	public boolean costStone(int stone, String cause) {
		return true;
	}

	@Override
	public boolean addStrength(int val, String cause) {
		return true;
	}

	@Override
	public boolean addStrength2(int val, String cause) {
		return true;
	}

	@Override
	public boolean decStrength(int val, String cause) {
		return true;
	}

	@Override
	public boolean addHonor(int val, String cause) {
		return true;
	}

	@Override
	public boolean addJoyBeans(int val, String cause) {
		return true;
	}

	@Override
	public boolean decJoyBeans(int val, String cause) {
		return true;
	}
	
	public RobotTemplate getTemplate() {
		return template;
	}
	
}
